package test2;

import java.util.ArrayList;
import java.util.PriorityQueue;

/**
 * @Author Jun jie Yi
 * 2022/4/6 10:20
 */

public class Demo {
    public static void main(String[] args) {
        ArrayList<Integer> a = new ArrayList<>();
        a.add(1);
        a.add(2);
        a.add(7);
        minMax(a, 2, 3);
    }

    /**
     * @param a 数组
     * @param k 减的次数
     * @param x 被减的数
     * @return
     */
    public static int minMax(ArrayList<Integer> a, int k, int x) {
        //大顶堆
        PriorityQueue<Integer> maxHeap = new PriorityQueue<Integer>(a.size(), (i1, i2) -> i2 - i1);
        for (int i = 0; i < a.size(); i++) {
            maxHeap.offer(a.get(i));
        }
        //操作 3 次，每次拿出最大值减
        for (int i = 0; i < k; i++) {
            Integer maxVal = maxHeap.poll();
            maxVal = maxVal - x;
            maxHeap.offer(maxVal);
        }
        return maxHeap.poll();
    }
}
